package com.ttpai.boot.lbs.controller;

import com.ttpai.boot.lbs.util.JsoupUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Kail on 2017/5/24.
 */
@Controller
@RequestMapping("/lbs/webapi")
public class LBSWebApiController {

    private static final String AK = "ZGRIsTdTkiH3W5qZtvZIikgEuRCiyKky";
    private static final String RIDING = "http://api.map.baidu.com/routematrix/v2/riding?origins=%s&destinations=%s&output=json&ak=" + AK;

    /**
     * /lbs/webapi/route_matrix?origins=31.172125,121.391189&destinations=31.16131,121.356011;31.24026,121.436751
     */
    @ResponseBody
    @RequestMapping("/route_matrix")
    public String routeMatrixRiding(@RequestParam("origins") String origins, @RequestParam("destinations") String destinations, String callback) {
        String url = String.format(RIDING, origins, destinations.replace(";", "|"));
        System.out.println(url);

        String bodyText = JsoupUtil.getBodyText(url);
        if (StringUtils.isBlank(callback)) {
            return bodyText;
        } else {
            return callback + "(" + bodyText + ")";
        }
    }

}
